home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / parseargs.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-01-07  |  4.1 KB  |  188 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. from types import *
  5. from cStringIO import StringIO
  6.  
  7. def formatDefinitions(options, COLS):
  8.     s = StringIO()
  9.     indent = ' ' * 10
  10.     width = COLS - 11
  11.     if width < 15:
  12.         width = COLS - 2
  13.         indent = ' '
  14.     
  15.     for longname, default, doc in options:
  16.         s.write('--' + longname + ' <arg>\n')
  17.         if default is not None:
  18.             doc += ' (defaults to ' + repr(default) + ')'
  19.         
  20.         i = 0
  21.         for word in doc.split():
  22.             if i == 0:
  23.                 s.write(indent + word)
  24.                 i = len(word)
  25.                 continue
  26.             if i + len(word) >= width:
  27.                 s.write('\n' + indent + word)
  28.                 i = len(word)
  29.                 continue
  30.             s.write(' ' + word)
  31.             i += len(word) + 1
  32.         
  33.         s.write('\n\n')
  34.     
  35.     return s.getvalue()
  36.  
  37.  
  38. def usage(str):
  39.     raise ValueError(str)
  40.  
  41.  
  42. def parseargs(argv, options, minargs = None, maxargs = None):
  43.     config = { }
  44.     longkeyed = { }
  45.     for option in options:
  46.         (longname, default, doc) = option
  47.         longkeyed[longname] = option
  48.         config[longname] = default
  49.     
  50.     options = []
  51.     args = []
  52.     pos = 0
  53.     while pos < len(argv):
  54.         if argv[pos][:2] != '--':
  55.             args.append(argv[pos])
  56.             pos += 1
  57.             continue
  58.         if pos == len(argv) - 1:
  59.             usage('parameter passed in at end with no value')
  60.         
  61.         (key, value) = (argv[pos][2:], argv[pos + 1])
  62.         pos += 2
  63.         if not longkeyed.has_key(key):
  64.             usage('unknown key --' + key)
  65.         
  66.         (longname, default, doc) = longkeyed[key]
  67.         
  68.         try:
  69.             t = type(config[longname])
  70.             if t is NoneType or t is StringType:
  71.                 config[longname] = value
  72.             elif t in (IntType, LongType):
  73.                 config[longname] = long(value)
  74.             elif t is FloatType:
  75.                 config[longname] = float(value)
  76.             elif not 0:
  77.                 raise AssertionError
  78.         continue
  79.         except ValueError:
  80.             e = None
  81.             usage('wrong format of --%s - %s' % (key, str(e)))
  82.             continue
  83.         
  84.  
  85.         None<EXCEPTION MATCH>ValueError
  86.     for key, value in config.items():
  87.         if value is None:
  88.             usage('Option --%s is required.' % key)
  89.             continue
  90.     
  91.     if minargs is not None and len(args) < minargs:
  92.         usage('Must supply at least %d args.' % minargs)
  93.     
  94.     if maxargs is not None and len(args) > maxargs:
  95.         usage('Too many args - %d max.' % maxargs)
  96.     
  97.     return (config, args)
  98.  
  99.  
  100. def test_parseargs():
  101.     if not parseargs(('d', '--a', 'pq', 'e', '--b', '3', '--c', '4.5', 'f'), (('a', 'x', ''), ('b', 1, ''), ('c', 2.2999999999999998, ''))) == ({
  102.         'a': 'pq',
  103.         'b': 3,
  104.         'c': 4.5 }, [
  105.         'd',
  106.         'e',
  107.         'f']):
  108.         raise AssertionError
  109.     if not parseargs([], [
  110.         ('a', 'x', '')]) == ({
  111.         'a': 'x' }, []):
  112.         raise AssertionError
  113.     if not parseargs([
  114.         '--a',
  115.         'x',
  116.         '--a',
  117.         'y'], [
  118.         ('a', '', '')]) == ({
  119.         'a': 'y' }, []):
  120.         raise AssertionError
  121.     
  122.     try:
  123.         parseargs([], [
  124.             ('a', 'x', '')])
  125.     except ValueError:
  126.         pass
  127.  
  128.     
  129.     try:
  130.         parseargs([
  131.             '--a',
  132.             'x'], [])
  133.     except ValueError:
  134.         pass
  135.  
  136.     
  137.     try:
  138.         parseargs([
  139.             '--a'], [
  140.             ('a', 'x', '')])
  141.     except ValueError:
  142.         pass
  143.  
  144.     
  145.     try:
  146.         parseargs([], [], 1, 2)
  147.     except ValueError:
  148.         pass
  149.  
  150.     if not parseargs([
  151.         'x'], [], 1, 2) == ({ }, [
  152.         'x']):
  153.         raise AssertionError
  154.     if not parseargs([
  155.         'x',
  156.         'y'], [], 1, 2) == ({ }, [
  157.         'x',
  158.         'y']):
  159.         raise AssertionError
  160.     
  161.     try:
  162.         parseargs([
  163.             'x',
  164.             'y',
  165.             'z'], [], 1, 2)
  166.     except ValueError:
  167.         pass
  168.  
  169.     
  170.     try:
  171.         parseargs([
  172.             '--a',
  173.             '2.0'], [
  174.             ('a', 3, '')])
  175.     except ValueError:
  176.         pass
  177.  
  178.     
  179.     try:
  180.         parseargs([
  181.             '--a',
  182.             'z'], [
  183.             ('a', 2.1000000000000001, '')])
  184.     except ValueError:
  185.         pass
  186.  
  187.  
  188.